using Microsoft.VisualStudio.TestTools.UnitTesting;
using StockholmsStad.Data.Interfaces;
using StockholmsStad.Runtime.Serialization.DataContract;

namespace StockholmsStad.Data.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class TestUnitProperties : TestUnitBase
    {
        // before running each test 
        [TestInitialize()]
        public void MyTestInitialize()
        {
            BeginTransaction();
        }

        // after each test has run
        [TestCleanup()]
        public void MyTestCleanup()
        {
            EndTransaction();
        }

        [TestMethod]
        public void RetrievePropertiesTest()
        {
            IPropertiesAgent agent = AgentFactory.GetPropertiesAgent();
            ParVisProperties properties = agent.GetProperties(context);
            Assert.AreNotEqual(0, properties.ImpedimentDocValidMonths);
            Assert.AreNotEqual(0, properties.IndividualImpedimentDocReminderAtDays);
            Assert.AreNotEqual(0, properties.IndividualImpedimentDocRemoveBookingAtDays);
            Assert.AreNotEqual(0, properties.IndividualPreliminaryBookingEarliestDays);
            Assert.AreNotEqual(0, properties.ReminderOfDocumentsIndividualBookingDays);
            Assert.AreNotEqual(0, properties.ReminderOfServiceOrdinaryBookingDays);
            Assert.AreNotEqual(0, properties.OrdinaryImpedimentDocReminderAtDays);
            Assert.AreNotEqual(0, properties.OrdinaryImpedimentDocRemoveBookingAtDays);
            Assert.AreNotEqual(0, properties.OrdinaryPreliminaryBookingEarliestDays);
            Assert.AreNotEqual(0, properties.ApplicationChangeDateIndividualBookingDays);
            Assert.AreNotEqual(0, properties.ApplicationChangeDateOrdinaryBookingDays);
        }

        [TestMethod]
        public void GetAndSetProperties()
        {
            IPropertiesAgent agent = AgentFactory.GetPropertiesAgent();

            // Get original properties
            ParVisProperties originalProperties = agent.GetProperties(context);

            // Update properties
            var newProperties = new ParVisProperties
                                    {
                                        ApplicationChangeDateIndividualBookingDays = 5,
                                        ApplicationChangeDateOrdinaryBookingDays = 5,
                                        ReminderOfDocumentsIndividualBookingDays = 6,
                                        ReminderOfServiceOrdinaryBookingDays = 6,
                                        OrdinaryImpedimentDocReminderAtDays = 6,
                                        OrdinaryImpedimentDocRemoveBookingAtDays = 3,
                                        OrdinaryPreliminaryBookingEarliestDays = 10,
                                        ImpedimentDocValidMonths = 2,
                                        IndividualImpedimentDocReminderAtDays = 4,
                                        IndividualImpedimentDocRemoveBookingAtDays = 8,
                                        IndividualPreliminaryBookingEarliestDays = 10
                                    };

            agent.SetProperties(context, newProperties);
            var updatedProperties = agent.GetProperties(context);

            ArePropertiesEqual(newProperties, updatedProperties);

            // Set properties back to original
            agent.SetProperties(context, originalProperties);
            var updatedOriginalProperties = agent.GetProperties(context);

            ArePropertiesEqual(originalProperties, updatedOriginalProperties);
        }

        private static void ArePropertiesEqual(ParVisProperties expected, ParVisProperties actual)
        {
            Assert.AreEqual(expected.ApplicationChangeDateIndividualBookingDays, actual.ApplicationChangeDateIndividualBookingDays);
            Assert.AreEqual(expected.ApplicationChangeDateOrdinaryBookingDays, actual.ApplicationChangeDateOrdinaryBookingDays);
            Assert.AreEqual(expected.ReminderOfDocumentsIndividualBookingDays, actual.ReminderOfDocumentsIndividualBookingDays);
            Assert.AreEqual(expected.ReminderOfServiceOrdinaryBookingDays, actual.ReminderOfServiceOrdinaryBookingDays);
            Assert.AreEqual(expected.OrdinaryImpedimentDocReminderAtDays, actual.OrdinaryImpedimentDocReminderAtDays);
            Assert.AreEqual(expected.OrdinaryImpedimentDocRemoveBookingAtDays, actual.OrdinaryImpedimentDocRemoveBookingAtDays);
            Assert.AreEqual(expected.OrdinaryPreliminaryBookingEarliestDays, actual.OrdinaryPreliminaryBookingEarliestDays);
            Assert.AreEqual(expected.ImpedimentDocValidMonths, actual.ImpedimentDocValidMonths);
            Assert.AreEqual(expected.IndividualImpedimentDocReminderAtDays, actual.IndividualImpedimentDocReminderAtDays);
            Assert.AreEqual(expected.IndividualImpedimentDocRemoveBookingAtDays, actual.IndividualImpedimentDocRemoveBookingAtDays);
            Assert.AreEqual(expected.IndividualPreliminaryBookingEarliestDays, actual.IndividualPreliminaryBookingEarliestDays);
        }
    }
}